<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"lanqilu.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":true},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#395ca3","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="参考《C Primer Plus》 第六版">
<meta property="og:type" content="article">
<meta property="og:title" content="C语言学习笔记">
<meta property="og:url" content="https://lanqilu.github.io/2020/05/25/C/C%E8%AF%AD%E8%A8%80%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="Halo">
<meta property="og:description" content="参考《C Primer Plus》 第六版">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://img.whl123456.top/image/image-20200525231404657.png">
<meta property="og:image" content="http://img.whl123456.top/image/image-20200526211617340.png">
<meta property="og:image" content="http://img.whl123456.top/image/image-20200526215203116.png">
<meta property="og:image" content="http://img.whl123456.top/image/image-20200528222920536.png">
<meta property="article:published_time" content="2020-05-25T15:06:36.000Z">
<meta property="article:modified_time" content="2020-07-06T05:56:54.079Z">
<meta property="article:author" content="Lanqilu">
<meta property="article:tag" content="C">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://img.whl123456.top/image/image-20200525231404657.png">

<link rel="canonical" href="https://lanqilu.github.io/2020/05/25/C/C%E8%AF%AD%E8%A8%80%E7%AC%94%E8%AE%B0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>C语言学习笔记 | Halo</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-161500949-1"></script>
    <script data-pjax>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-161500949-1');
      }
    </script>


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?256151d1651e9d73ec980b2fc69de8f6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Halo</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">39</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">85</span></a>

  </li>
        <li class="menu-item menu-item-python">

    <a href="/categories/Python/" rel="section"><i class="fa fa-fw fa-code"></i>Python</a>

  </li>
        <li class="menu-item menu-item-java">

    <a href="/categories/Java/" rel="section"><i class="fa fa-fw fa-code"></i>Java</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://lanqilu.github.io/2020/05/25/C/C%E8%AF%AD%E8%A8%80%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="http://img.whl123456.top/image/avatar.jpg">
      <meta itemprop="name" content="Lanqilu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Halo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C语言学习笔记
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-05-25 23:06:36" itemprop="dateCreated datePublished" datetime="2020-05-25T23:06:36+08:00">2020-05-25</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-06 13:56:54" itemprop="dateModified" datetime="2020-07-06T13:56:54+08:00">2020-07-06</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <div class="note info no-icon">
            <p>参考《C Primer Plus》 第六版</p>
          </div>

<a id="more"></a>

<hr>
<h2 id="C语言概述"><a href="#C语言概述" class="headerlink" title="C语言概述"></a>C语言概述</h2><p>简单的C程序示例</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> num;</span><br><span class="line">    num = <span class="number">1</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"Hello World!\n"</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"My favorite number is %d because it is first.\n"</span>,num);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="http://img.whl123456.top/image/image-20200525231404657.png" alt="image-20200525231404657"></p>
<hr>
<h3 id="include-指令和头文件"><a href="#include-指令和头文件" class="headerlink" title="#include 指令和头文件"></a><code>#include</code> 指令和头文件</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt; //包含另一个文件</span></span></span><br></pre></td></tr></table></figure>

<p>该行告诉编译器把 <code>stdio.h</code> 中的内容包含在当前程序中。</p>
<p><code>stdio.h</code> 是 C 编译器软件包的标准部分，它提供键盘<strong>输入</strong>和屏幕<strong>输出</strong>的支持。</p>
<blockquote>
<p>stdio的全称是standard input output，h是head的缩写，.h是头文件的文件格式</p>
<p>在C++中<code>#include&lt;stdio.h&gt;</code>等价写法<code>#include&lt;cstdio&gt;</code></p>
</blockquote>
<p>作用相当于把 <code>stdio.h</code> 文件中的所有内容都输入该行所在的位置。</p>
<p><code>#include</code> 这行代码是一条 C <strong>预处理器指令</strong></p>
<h3 id="main-函数"><a href="#main-函数" class="headerlink" title="main()函数"></a><code>main()</code>函数</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span>  <span class="comment">//主函数</span></span></span><br></pre></td></tr></table></figure>

<p>C 程序包含一个或多个函数，它们是 C 程序的基本模块。</p>
<p>主函数是一个程序的入口位置，整个程序从主函数开始执行。一个程序最多只能有一个主函数。</p>
<p>圆括号表明 main()是一个函数名。int 表明 main()函数返回一个整数，void 表明 main()不带任何参数。</p>
<p>main 是一个极其普通的名称，但是这是唯一的选择</p>
<blockquote>
<p>C 程序一定从 main()函数开始执行（目前不必考虑例外的情况）。</p>
<p>除了 main()函数，你可以任意命名其他函数，而且 main()函数必须是开始的函数。</p>
</blockquote>
<h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><p>在程序中，被<code>/*  */</code>两个符号括起来的部分是程序的注释。</p>
<p>C99 新增了另一种风格的注释，普遍用于 C++和 Java。这种新风格使用<code>//</code>符号创建注释，仅限于单行。</p>
<h3 id="花括号、函数体和块"><a href="#花括号、函数体和块" class="headerlink" title="花括号、函数体和块"></a>花括号、函数体和块</h3><p>一般而言，所有的 C 函数都使用花括号<code>{}</code>标记函数体的开始和结束。</p>
<p>花括号还可用于把函数中的多条语句合并为一个单元或块。</p>
<h3 id="声明"><a href="#声明" class="headerlink" title="声明"></a>声明</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> num;  <span class="comment">//声明</span></span><br></pre></td></tr></table></figure>

<p>以前的 C 语言，还要求把变量声明在块的顶部，其他语句不能在任何声明的前面。</p>
<p>C99 和 C11 遵循 C++的惯例，可以把声明放在块中的任何位置。尽管如此，首次使用变量之前一定要先声明它。</p>
<blockquote>
<p>命名</p>
<p>可以用小写字母、大写字母、数字和下划线（_）来命名。<br>而且，名称的第 1 个字符必须是字符或下划线，不能是数字。</p>
<p>操作系统和 C 库经常使用以一个或两个下划线字符开始的标识符（如，_kcab），因此最好避免在自己的程序中使用这种名称。</p>
<p>C 语言的名称区分大小写，即把一个字母的大写和小写视为两个不同的字符。</p>
</blockquote>
<p>声明变量的 4 个理由：</p>
<ul>
<li>把所有的变量放在一处，方便读者查找和理解程序的用途。</li>
<li>声明变量会促使你在编写程序之前做一些计划。</li>
<li>声明变量有助于发现隐藏在程序中的小错误，如变量名拼写错误。</li>
<li>如果事先未声明变量，C 程序将无法通过编译。</li>
</ul>
<h3 id="赋值"><a href="#赋值" class="headerlink" title="赋值"></a>赋值</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">num = <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<p>在执行<code>int num;</code>声明时，编译器在计算机内存中为变量<code>num</code>预留了空间，然后在执行这行赋值表达式语句时，把值储存在之前预留的位置。</p>
<p>赋值表达式语句从右侧把值赋到左侧</p>
<h3 id="printf-函数"><a href="#printf-函数" class="headerlink" title="printf()函数"></a><code>printf()</code>函数</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">printf</span>(<span class="string">"My favorite number is %d because it is first.\n"</span>, num);</span><br></pre></td></tr></table></figure>

<p>圆括号表明 <code>printf</code> 是一个函数名。</p>
<p>实际参数（简称实参）是传递给函数的特定值，形式参数（简称形参）是函数中用于储存值的变量</p>
<p><code>\n</code>组合代表一个换行符，换行符是一个转义序列（用于代表难以表示或无法输入的字符）</p>
<p>参数中的<code>%d</code>相当于是一个占位符，其作用是指明输出 <code>num</code> 值的位置，<code>%</code>提醒程序，要在该处打印一个变量，<code>d</code>表明把变量作为十进制整数打印</p>
<p><code>printf()</code>函数名中的 <code>f</code> 提醒用户，这是一种格式化打印函数。</p>
<h3 id="return-语句"><a href="#return-语句" class="headerlink" title="return 语句"></a><code>return</code> 语句</h3><p>有返回值的 C 函数要有 <code>return</code> 语句</p>
<p>如果遗漏 <code>main()</code>函数中的 <code>return</code> 语句，程序在运行至最外面的右花括号（<code>}</code>）时会返回 <code>0</code>。</p>
<h2 id="数据"><a href="#数据" class="headerlink" title="数据"></a>数据</h2><p>有些数据类型在程序使用之前已经预先设定好了，在整个程序的运行过程中没有变化，这些称为常量（constant）</p>
<p>其他数据类型在程序运行期间可能会改变或被赋值，这些称为变量（variable）</p>
<p><img src="http://img.whl123456.top/image/image-20200526211617340.png" alt="C语言数据类型关键字"></p>
<ul>
<li><p>用 <code>int</code> 关键字来表示基本的整数类型</p>
</li>
<li><p><code>long</code>、<code>short</code> 和 <code>unsigned</code>和 C90 新增的 <code>signed</code> 用于提供基本整数类型的变式，例如 <code>unsigned short int</code> 和 <code>long long int</code>。</p>
</li>
<li><p><code>char</code> 关键字用于指定字母和其他字符（如，<code>#</code>、<code>$</code>、<code>%</code>和<code>*</code>）,<code>char</code> 类型也可以表示较小的整数</p>
</li>
<li><p><code>float</code>、<code>double</code> 和 <code>long double</code> 表示带小数点的数</p>
</li>
<li><p><code>_Bool</code>类型表示布尔值（<code>true</code> 或 <code>false</code>）</p>
</li>
<li><p><code>_complex</code> 和<code>_Imaginary</code> 分别表示复数和虚数</p>
</li>
</ul>
<blockquote>
<p><strong>位、字节和字</strong></p>
<p>最小的存储单元是位（bit），可以储存 0 或 1</p>
<p>字节（byte）是常用的计算机存储单位。对于几乎所有的机器，1 字节均为 8 位。</p>
<p>字（word）是设计计算机时给定的自然存储单位。对于 8 位的微型计算机（如，最初的苹果机），1 个字长只有 8 位。从那以后，个人计算机字长增至 16 位、32 位，直到目前的 64 位。</p>
</blockquote>
<h3 id="整数和浮点数"><a href="#整数和浮点数" class="headerlink" title="整数和浮点数"></a>整数和浮点数</h3><p>整数和数学的概念一样，在 C 语言中，整数是没有小数部分的数</p>
<p>浮点数与数学中实数的概念差不多。2.75、3.16E7、7.00 和 2e-8 都是浮点数。3.16E7 表示 3.16×10<sup>7</sup></p>
<ul>
<li>整数没有小数部分，浮点数有小数部分。</li>
<li>浮点数可以表示的范围比整数大。</li>
<li>对于一些算术运算（如，两个很大的数相减），浮点数损失的精度更多。</li>
<li>因为在任何区间内（如，1.0 到 2.0 之间）都存在无穷多个实数，所以计算机的浮点数不能表示区间内所有的值。浮点数通常只是实际值的近似值。例如，7.0 可能被储存为浮点值 6.99999。</li>
<li>过去，浮点运算比整数运算慢。不过，现在许多 CPU 都包含浮点处理器，缩小了速度上的差距。</li>
</ul>
<h3 id="C-语言基本数据类型"><a href="#C-语言基本数据类型" class="headerlink" title="C 语言基本数据类型"></a>C 语言基本数据类型</h3><h4 id="int-类型"><a href="#int-类型" class="headerlink" title="int 类型"></a><code>int</code> 类型</h4><p><code>int</code> 类型是有符号整型，即 <code>int</code> 类型的值必须是整数，可以是正整数、负整数或零。</p>
<p>一般而言，储存一个 <code>int</code> 要占用一个机器字长</p>
<p>ISO C 规定 <code>int</code> 的取值范围最小为-32768～32767</p>
<p>一般而言，系统用一个特殊位的值表示有符号整数的正负号</p>
<h5 id="声明-int-变量"><a href="#声明-int-变量" class="headerlink" title="声明 int 变量"></a>声明 <code>int</code> 变量</h5><p>要声明多个变量，可以单独声明每个变量，也可在 int 后面列出多个变量名，变量名之间用逗号分隔。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> erns; </span><br><span class="line"><span class="keyword">int</span> hogs, cows, goats;</span><br></pre></td></tr></table></figure>

<p>提供值方法：1. 赋值；2. 通过函数，如<code>scanf()</code>获得值；3. 初始化变量</p>
<p>初始化（initialize）变量就是为变量赋一个初始值。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> hogs = <span class="number">21</span>; </span><br><span class="line"><span class="keyword">int</span> cows = <span class="number">32</span>, goats = <span class="number">14</span>; </span><br><span class="line"><span class="keyword">int</span> dogs, cats = <span class="number">94</span>; <span class="comment">/* 有效，但是这种格式很糟糕 */</span></span><br></pre></td></tr></table></figure>

<p>以上示例的最后一行，只初始化了 cats，并未初始化 dogs。这种写法很容易让人误认为 dogs 也被初始化为 94，所以最好不要把初始化的变量和未初始化的变量放在同一条声明中。</p>
<h5 id="打印-int-值"><a href="#打印-int-值" class="headerlink" title="打印 int 值"></a>打印 <code>int</code> 值</h5><p>可以使用 <code>printf()</code>函数打印 <code>int</code> 类型的值</p>
<p><code>%d</code>称为<strong>转换说明</strong>，它指定了 <code>printf()</code>应使用什么格式来显示一个值。格式化字符串中的每个<code>%d</code> 都与待打印变量列表中相应的 <code>int</code> 值匹配。</p>
<h5 id="八进制和十六进制"><a href="#八进制和十六进制" class="headerlink" title="八进制和十六进制"></a>八进制和十六进制</h5><p>在 C 语言中，用特定的前缀表示使用哪种进制。</p>
<p>0x 或 0X 前缀表示十六进制值，所以十进制数 16 表示成十六进制是 0x10 或 0X10。</p>
<p>与此类似，0 前缀表示八进制。例如，十进制数 16 表示成八进制是 020。</p>
<p>以十进制显示数字，使用<code>%d</code>；以八进制显示数字，使用<code>%o</code>；以十六进制显示数字，使用<code>%x</code>。</p>
<p>另外，要显示各进制数的前缀0、0x 和 0X，必须分别使用<code>%#o</code>、<code>%#x</code>、<code>%#X</code></p>
<h5 id="其他整数类型"><a href="#其他整数类型" class="headerlink" title="其他整数类型"></a>其他整数类型</h5><p>C 语言提供 3 个附属关键字修饰基本整数类型：<code>short</code>、<code>long</code> 和 <code>unsigned</code></p>
<ul>
<li><p><code>short int</code> 类型（或者简写为 <code>short</code>）占用的存储空间可能比 <code>int</code> 类型少，常用于较小数值的场合以节省空间。与 <code>int</code> 类似，<code>short</code> 是有符号类型。</p>
</li>
<li><p><code>long int</code> 或 <code>long</code> 占用的存储空间可能比 <code>int</code> 多，适用于较大数值的场合。与 <code>int</code> 类似，<code>long</code>是有符号类型。</p>
</li>
<li><p><code>long long int</code> 或 <code>long long</code>（C99 标准加入）占用的储存空间可能比 <code>long</code> 多，适用于更大数值的场合。该类型<strong>至少占 64 位</strong>。与 <code>int</code> 类似，<code>long long</code> 是有符号类型。</p>
</li>
<li><p><code>unsigned int</code> 或 <code>unsigned</code> 只用于<strong>非负值</strong>的场合。这种类型与有符号类型表示的范围不同。例如，16 位 <code>unsigned int</code> 允许的取值范围是 0～65535，而不是-32768～32767。用于表示正负号的位现在用于表示另一个二进制位，所以无符号整型可以表示更大的数。</p>
</li>
<li><p>在 C90 标准中，添加了 <code>unsigned long int</code> 或 <code>unsigned long</code> 和 <code>unsigned int</code> 或 <code>unsigned short</code> 类型。C99 标准又添加了 <code>unsigned long long int</code> 或 <code>unsigned long long</code>。</p>
</li>
<li><p>在任何有符号类型前面添加关键字 <code>signed</code>，可强调使用有符号类型的意图。例如，<code>short</code>、<code>short int</code>、<code>signed short</code>、<code>signed short int</code> 都表示同一种类型。</p>
</li>
</ul>
<p><code>int</code> 类型那么多，应该如何选择？</p>
<p>首先，考虑 <code>unsigned</code> 类型。这种类型的数常用于计数，因为计数不用负数。而且，<code>unsigned</code> 类型可以表示更大的正数。 </p>
<p>如果一个数超出了 <code>int</code> 类型的取值范围，且在 <code>long</code> 类型的取值范围内时，使用 <code>long</code> 类型。</p>
<p>然而，对于那些 <code>long</code> 占用的空间比 <code>int</code> 大的系统，使用 <code>long</code> 类型会减慢运算速度。因此，如非必要，请不要使用 <code>long</code> 类型。另外要注意一点：如果在 <code>long</code> 类型和 <code>int</code> 类型占用空间相同的机器上编写代码，当确实需要 32 位的整数时，应使用 <code>long</code> 类型而不是 <code>int</code> 类型，以便把程序移植到 16 位机后仍然可以正常工作。类似地，如果确实需要 64 位的整数，应使用 <code>long long</code> 类型。 </p>
<p>如果在 <code>int</code> 设置为 32 位的系统中要使用 16 位的值，应使用 <code>short</code> 类型以节省存储空间。通常，只有当程序使用相对于系统可用内存较大的整型数组时，才需要重点考虑节省空间的问题。使用 <code>short</code> 类型的另一个原因是，计算机中某些组件使用的硬件寄存器是 16 位。</p>
<h4 id="char类型"><a href="#char类型" class="headerlink" title="char类型"></a><code>char</code>类型</h4><p><code>char</code> 类型用于储存字符（如，字母或标点符号），但是从技术层面看，<code>char</code> 是整数类型。</p>
<h5 id="声明-char-类型变量"><a href="#声明-char-类型变量" class="headerlink" title="声明 char 类型变量"></a>声明 <code>char</code> 类型变量</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> response; </span><br><span class="line"><span class="keyword">char</span> itable, latan;</span><br></pre></td></tr></table></figure>

<h5 id="字符常量和初始化"><a href="#字符常量和初始化" class="headerlink" title="字符常量和初始化"></a>字符常量和初始化</h5><p>在 C 语言中，用单引号括起来的单个字符被称为字符常量（character constant）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> broiled;  <span class="comment">/* 声明一个 char 类型的变量 */</span> </span><br><span class="line">broiled = <span class="string">'T'</span>; <span class="comment">/* 为其赋值，正确 */</span> </span><br><span class="line">broiled = T;  <span class="comment">/* 错误！此时 T 是一个变量 */</span> </span><br><span class="line">broiled = <span class="string">"T"</span>; <span class="comment">/* 错误！此时"T"是一个字符串 */</span></span><br><span class="line"><span class="keyword">char</span> grade = <span class="number">65</span>; <span class="comment">/* 对于 ASCII，这样做没问题，但这是一种不好的编程风格 */</span></span><br></pre></td></tr></table></figure>

<h5 id="符号常量和-const常量"><a href="#符号常量和-const常量" class="headerlink" title="符号常量和 const常量"></a>符号常量和 const常量</h5><p>符号常量通俗地讲就是“替换”，即用一个标识符来替代常量，又称为“宏定义”或者“宏替换”。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pi 3.14</span></span><br></pre></td></tr></table></figure>

<p>另一种定义常量的方法是使用 <code>const</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const double pi &#x3D; 3.14;</span><br></pre></td></tr></table></figure>

<p>于是在程序中凡是使用pi的地方将在程序执行前全部自动替换为3.14。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pi 3.14</span></span><br><span class="line"><span class="comment">//const double pi = 3.14;</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">double</span> r = <span class="number">3</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%f\n"</span>, pi * r * r);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="非打印字符"><a href="#非打印字符" class="headerlink" title="非打印字符"></a>非打印字符</h5><ol>
<li>使用 ASCII 码</li>
<li>用特殊的符号序列表示一些特殊的字符。这些符号序列叫作转义序列（escape sequence）</li>
</ol>
<p><img src="http://img.whl123456.top/image/image-20200526215203116.png" alt="转义序列"></p>
<h4 id="Bool-类型"><a href="#Bool-类型" class="headerlink" title="_Bool 类型"></a><code>_Bool</code> 类型</h4><p>C99 标准添加了<code>_Bool</code> 类型，用于表示布尔值，即逻辑值 true 和 false。因为 C 语言用值 1 表示<br>true，值 0 表示 false，所以<code>_Bool</code> 类型实际上也是一种整数类型。但原则上它仅占用 1 位存储空间，<br>因为对 0 和 1 而言，1 位的存储空间足够了。</p>
<h4 id="可移植类型：stdint-h-和-inttypes-h"><a href="#可移植类型：stdint-h-和-inttypes-h" class="headerlink" title="可移植类型：stdint.h 和 inttypes.h"></a>可移植类型：<code>stdint.h</code> 和 <code>inttypes.h</code></h4><p>C 语言提供了许多有用的整数类型。但是，某些类型名在不同系统中的功能不一样。C99 新增了两个<br>头文件 <code>stdint.h</code> 和 <code>inttypes.h</code>，以确保 C 语言的类型在各系统中的功能相同。</p>
<h4 id="float、double-和-long-double"><a href="#float、double-和-long-double" class="headerlink" title="float、double 和 long double"></a><code>float</code>、<code>double</code> 和 <code>long double</code></h4><p>C 标准规定，<code>float</code> 类型必须至少能表示 6 位有效数字，且取值范围至少是 10<sup>-37</sup> ~10<sup>+37</sup>。</p>
<p><code>double</code> 类型和 <code>float</code> 类型的最小取值范围相同，但至少必须能表示 10 位有效数字。一般情况下，<code>double</code> 占用 64 位而不是 32 位。</p>
<p><code>long double</code>，以满足比 <code>double</code> 类型更高的精度要求。不过，C 只保证<code>long double</code> 类型至少与 <code>double</code> 类型的精度相同。</p>
<h4 id="类型大小"><a href="#类型大小" class="headerlink" title="类型大小"></a>类型大小</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//* typesize.c -- 打印类型大小 */ </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt; </span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span> </span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">     <span class="comment">/* C99 为类型大小提供%zd 转换说明 */</span> </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Type int has a size of %zd bytes.\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">int</span>)); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Type char has a size of %zd bytes.\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">char</span>)); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Type long has a size of %zd bytes.\n"</span>, <span class="keyword">sizeof</span>(<span class="keyword">long</span>)); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Type long long has a size of %zd bytes.\n"</span>, </span><br><span class="line">               <span class="keyword">sizeof</span>(<span class="keyword">long</span> <span class="keyword">long</span>)); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Type double has a size of %zd bytes.\n"</span>, </span><br><span class="line">              <span class="keyword">sizeof</span>(<span class="keyword">double</span>)); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Type long double has a size of %zd bytes.\n"</span>, </span><br><span class="line">              <span class="keyword">sizeof</span>(<span class="keyword">long</span> <span class="keyword">double</span>)); </span><br><span class="line">     <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>sizeof</code> 是 C 语言的内置运算符，以字节为单位给出指定类型的大小。</p>
<p>C99 和 C11 提供<code>%zd</code> 转换说明匹配 <code>sizeof</code> 的返回类型1。一些不支持 C99 和 C11 的编译器可用<code>%u</code> 或<code>%lu</code> 代替<code>%zd</code>。</p>
<p>该程序的输出如下： </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Type int has a size of 4 bytes. </span><br><span class="line">Type char has a size of 1 bytes. </span><br><span class="line">Type long has a size of 8 bytes. </span><br><span class="line">Type long long has a size of 8 bytes. </span><br><span class="line">Type double has a size of 8 bytes. </span><br><span class="line">Type long double has a size of 16 bytes.</span><br></pre></td></tr></table></figure>

<p>C 语言定义了 <code>char</code> 类型是 1 字节，所以 <code>char</code> 类型的大小一定是 1 字节。</p>
<p>而在 <code>char</code> 类型为 16 位、<code>double</code>类型为 64 位的系统中，<code>sizeof</code> 给出的 <code>double</code> 是 4 字节。</p>
<h3 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">double</span> r = <span class="number">12.56</span>;</span><br><span class="line">	<span class="keyword">int</span> a = <span class="number">3</span>, b = <span class="number">5</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (<span class="keyword">int</span>)r); <span class="comment">//r强制转换成int</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%d\n"</span>, a/b);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%.1f"</span>, (<span class="keyword">double</span>)a / (<span class="keyword">double</span>)b);<span class="comment">//%.1f保留一位小数输出</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="字符串和格式化输入-输出"><a href="#字符串和格式化输入-输出" class="headerlink" title="字符串和格式化输入/输出"></a>字符串和格式化输入/输出</h2><h3 id="字符串简介"><a href="#字符串简介" class="headerlink" title="字符串简介"></a>字符串简介</h3><p>字符串（character string）是一个或多个字符的序列。</p>
<p>双引号仅告知编译器它括起来的是字符串，正如单引号用于标识单个字符一样。</p>
<h3 id="char-类型数组和-null-字符"><a href="#char-类型数组和-null-字符" class="headerlink" title="char 类型数组和 null 字符"></a><code>char</code> 类型数组和 <code>null</code> 字符</h3><p>C 语言没有专门用于储存字符串的变量类型，字符串都被储存在 <code>char</code> 类型的数组中。数组由连续的存<br>储单元组成，字符串中的字符被储存在相邻的存储单元中，每个单元储存一个字符。</p>
<p>数组末尾位置的字符<code>\0</code>。这是空字符（null character），C 语言用它标记字符串的结束。空字符不是数字 0，它是非打印字符，其 ASCII 码值是（或等价于）0。</p>
<h3 id="strlen-函数"><a href="#strlen-函数" class="headerlink" title="strlen()函数"></a><code>strlen()</code>函数</h3><p><code>strlen()</code>函数给出字符串中的字符长度。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* praise2.c */</span> </span><br><span class="line"><span class="comment">// 如果编译器不识别%zd，尝试换成%u 或%lu。 </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt; </span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;      /* 提供 strlen()函数的原型 */ </span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PRAISE <span class="meta-string">"You are an extraordinary being."</span> </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span> </span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">     <span class="keyword">char</span> name[<span class="number">40</span>]; </span><br><span class="line"> </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"What's your name? "</span>); </span><br><span class="line">     <span class="built_in">scanf</span>(<span class="string">"%s"</span>, name); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Hello, %s. %s\n"</span>, name, PRAISE); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"Your name of %zd letters occupies %zd memory cells.\n"</span>, </span><br><span class="line">               <span class="built_in">strlen</span>(name), <span class="keyword">sizeof</span> name); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"The phrase of praise has %zd letters "</span>, </span><br><span class="line">               <span class="built_in">strlen</span>(PRAISE)); </span><br><span class="line">     <span class="built_in">printf</span>(<span class="string">"and occupies %zd memory cells.\n"</span>, <span class="keyword">sizeof</span> PRAISE); </span><br><span class="line"> </span><br><span class="line">     <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//What's your name? Serendipity Chance </span></span><br><span class="line"><span class="comment">//Hello, Serendipity. You are an extraordinary being. </span></span><br><span class="line"><span class="comment">//Your name of 11 letters occupies 40 memory cells. </span></span><br><span class="line"><span class="comment">//The phrase of praise has 31 letters and occupies 32 memory cells.</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>一般而言，C 把函数库中相关的函数归为一类，并为每类函数提供一个头文件。例如，<code>printf()</code>和<code>scanf()</code>都隶属标准输入和输出函数，使用 <code>stdio.h</code> 头文件。<code>string.h</code> 头文件中包含了 <code>strlen()</code>函数和其他一些与字符串相关的函数（如拷贝字符串的函数和字符串查找函数）。</p>
</blockquote>
<h3 id="常量和-C-预处理器"><a href="#常量和-C-预处理器" class="headerlink" title="常量和 C 预处理器"></a>常量和 C 预处理器</h3><h3 id="printf-和-scanf"><a href="#printf-和-scanf" class="headerlink" title="printf()和 scanf()"></a><code>printf()</code>和 <code>scanf()</code></h3><p><img src="http://img.whl123456.top/image/image-20200528222920536.png" alt="转换说明及其打印的输出结果"></p>
<h2 id="运算符、表达式和语句"><a href="#运算符、表达式和语句" class="headerlink" title="运算符、表达式和语句"></a>运算符、表达式和语句</h2><h3 id="基本运算符"><a href="#基本运算符" class="headerlink" title="基本运算符"></a>基本运算符</h3><h3 id="其他运算符"><a href="#其他运算符" class="headerlink" title="其他运算符"></a>其他运算符</h3><h3 id="表达式和语句"><a href="#表达式和语句" class="headerlink" title="表达式和语句"></a>表达式和语句</h3><h3 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h3><h3 id="带参数的函数"><a href="#带参数的函数" class="headerlink" title="带参数的函数"></a>带参数的函数</h3><h2 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h2><h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a><code>while</code>循环</h3><h3 id="for-循环"><a href="#for-循环" class="headerlink" title="for 循环"></a><code>for</code> 循环</h3><h3 id="出口条件循环：do-while"><a href="#出口条件循环：do-while" class="headerlink" title="出口条件循环：do while"></a>出口条件循环：<code>do while</code></h3><h2 id="分支和跳转"><a href="#分支和跳转" class="headerlink" title="分支和跳转"></a>分支和跳转</h2><h3 id="if-语句"><a href="#if-语句" class="headerlink" title="if 语句"></a><code>if</code> 语句</h3><h3 id="if-else-语句"><a href="#if-else-语句" class="headerlink" title="if else 语句"></a><code>if else</code> 语句</h3><h3 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h3><h3 id="条件运算符"><a href="#条件运算符" class="headerlink" title="条件运算符"></a>条件运算符</h3><h3 id="continue-和-break"><a href="#continue-和-break" class="headerlink" title="continue 和 break"></a><code>continue</code> 和 <code>break</code></h3><h3 id="多重选择：switch-和-break"><a href="#多重选择：switch-和-break" class="headerlink" title="多重选择：switch 和 break"></a>多重选择：<code>switch</code> 和 <code>break</code></h3><h3 id="goto-语句"><a href="#goto-语句" class="headerlink" title="goto 语句"></a><code>goto</code> 语句</h3><h2 id="字符输入-输出和输入验证"><a href="#字符输入-输出和输入验证" class="headerlink" title="字符输入/输出和输入验证"></a>字符输入/输出和输入验证</h2><h3 id="单字符-I-O：getchar-和-putchar"><a href="#单字符-I-O：getchar-和-putchar" class="headerlink" title="单字符 I/O：getchar()和 putchar()"></a>单字符 I/O：<code>getchar()</code>和 <code>putchar()</code></h3><h3 id="缓冲区"><a href="#缓冲区" class="headerlink" title="缓冲区"></a>缓冲区</h3><h3 id="结束键盘输入"><a href="#结束键盘输入" class="headerlink" title="结束键盘输入"></a>结束键盘输入</h3><h3 id="重定向和文件"><a href="#重定向和文件" class="headerlink" title="重定向和文件"></a>重定向和文件</h3><h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><h3 id="常用math函数"><a href="#常用math函数" class="headerlink" title="常用math函数"></a>常用math函数</h3><p>需要在程序开头加上<code>math.h</code>头文件</p>
<h4 id="fabs-double-x"><a href="#fabs-double-x" class="headerlink" title="fabs(double x)"></a><code>fabs(double x)</code></h4><p>该函数用于对 <code>double</code>型变量取绝对值</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">double</span> db = <span class="number">-12.56</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%.2f\n"</span>, <span class="built_in">fabs</span>(db)); <span class="comment">//输出：12.56</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="floor（double-x）和-ceil（double-x）"><a href="#floor（double-x）和-ceil（double-x）" class="headerlink" title="floor（double x）和 ceil（double x）"></a><code>floor（double x）</code>和 <code>ceil（double x）</code></h4><p>分别用于 <code>double</code>型变量的向下取整和向上取整</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">double</span> db1 = <span class="number">-12.56</span>;</span><br><span class="line">	<span class="keyword">double</span> db2 = <span class="number">12.56</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%.0f %.0f\n"</span>, <span class="built_in">floor</span>(db1),<span class="built_in">ceil</span>(db1)); <span class="comment">//输出：-13 -12</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"%.0f %.0f\n"</span>, <span class="built_in">floor</span>(db2),<span class="built_in">ceil</span>(db2)); <span class="comment">//输出：12 13</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="pow-double-r-double-p"><a href="#pow-double-r-double-p" class="headerlink" title="pow(double r,double p)"></a><code>pow(double r,double p)</code></h4><p>返回r的p次幂的值</p>
<h4 id="sqrt（double-x）"><a href="#sqrt（double-x）" class="headerlink" title="sqrt（double x）"></a><code>sqrt（double x）</code></h4><p>该函数用于返回 double型变量的算术平方根</p>
<h4 id="log（double-x）"><a href="#log（double-x）" class="headerlink" title="log（double x）"></a><code>log（double x）</code></h4><p>该函数用于返回 double型变量的以自然对数为底的对数</p>
<h4 id="sin（-double-x）、cos（-double-x）和tan（-double-x）"><a href="#sin（-double-x）、cos（-double-x）和tan（-double-x）" class="headerlink" title="sin（ double x）、cos（ double x）和tan（ double x）"></a><code>sin（ double x）、cos（ double x）和tan（ double x）</code></h4><h4 id="asin（double-x），-acos（double-x）F-atan-（double-x"><a href="#asin（double-x），-acos（double-x）F-atan-（double-x" class="headerlink" title="asin（double x）， acos（double x）F atan （double x)"></a><code>asin（double x）， acos（double x）F atan （double x)</code></h4><h4 id="round（double-x）"><a href="#round（double-x）" class="headerlink" title="round（double x）"></a><code>round（double x）</code></h4><h2 id="数组和指针"><a href="#数组和指针" class="headerlink" title="数组和指针"></a>数组和指针</h2><h3 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h3><h3 id="多维数组"><a href="#多维数组" class="headerlink" title="多维数组"></a>多维数组</h3><h3 id="指针和数组"><a href="#指针和数组" class="headerlink" title="指针和数组"></a>指针和数组</h3><h3 id="函数、数组和指针"><a href="#函数、数组和指针" class="headerlink" title="函数、数组和指针"></a>函数、数组和指针</h3><h3 id="指针操作"><a href="#指针操作" class="headerlink" title="指针操作"></a>指针操作</h3><h2 id="字符串和字符串函数"><a href="#字符串和字符串函数" class="headerlink" title="字符串和字符串函数"></a>字符串和字符串函数</h2><h2 id="枚举"><a href="#枚举" class="headerlink" title="枚举"></a>枚举</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> color &#123;RED,YELLO,GREEN&#125;;<span class="comment">//RED = 0,YELLO = 1,GREEN = 3...</span></span><br><span class="line"><span class="comment">// enum 枚举类型名字 &#123;名字0,…,名字n&#125;;</span></span><br><span class="line"><span class="comment">// 枚举类型名字可忽略</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">f</span><span class="params">(<span class="keyword">enum</span> color c)</span></span>;</span><br></pre></td></tr></table></figure>

<p>用枚举而不是定义独立的const in变量</p>
<p>常量符号，它们的类型只能是int</p>
<h3 id="技巧：自动计数的枚举"><a href="#技巧：自动计数的枚举" class="headerlink" title="技巧：自动计数的枚举"></a>技巧：自动计数的枚举</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">enum</span> COLOR &#123;RED, YELLOW, GREEN, NumCOLORS&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> <span class="keyword">const</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>枚举中各常量值依次递加，故最后一个值可代表常量个数</p>
<h3 id="指定枚举量的值"><a href="#指定枚举量的值" class="headerlink" title="指定枚举量的值"></a>指定枚举量的值</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> COLOR &#123;RED=<span class="number">1</span>, YELLOW, GREEN=<span class="number">5</span>, NumCOLORS&#125;;<span class="comment">//YELLOW = 2,NumCOLORS?</span></span><br></pre></td></tr></table></figure>



<h2 id="存储类别、链接和内存管理"><a href="#存储类别、链接和内存管理" class="headerlink" title="存储类别、链接和内存管理"></a>存储类别、链接和内存管理</h2><h2 id="文件输入-输出"><a href="#文件输入-输出" class="headerlink" title="文件输入/输出"></a>文件输入/输出</h2><h2 id="结构和其他数据形式"><a href="#结构和其他数据形式" class="headerlink" title="结构和其他数据形式"></a>结构和其他数据形式</h2><p>表达数据比较复杂，但需要一个整体来表达，使用结构——一个复合的数据类型</p>
<h3 id="声明结构类型"><a href="#声明结构类型" class="headerlink" title="声明结构类型"></a>声明结构类型</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> month;</span><br><span class="line">    <span class="keyword">int</span> day;</span><br><span class="line">    <span class="keyword">int</span> year;</span><br><span class="line">&#125;;<span class="comment">//注意分号</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> <span class="keyword">const</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">today</span>;</span></span><br><span class="line">    </span><br><span class="line">    today.month = <span class="number">06</span>;</span><br><span class="line">    today.day = <span class="number">01</span>;</span><br><span class="line">    today.year = <span class="number">2020</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"Today's date is %i-%i-%i.\n"</span>,</span><br><span class="line">           today.year,today.month,today.day);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>另两种声明结构的形式</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> x;</span><br><span class="line">    <span class="keyword">int</span> y;</span><br><span class="line">&#125;p1,p2;<span class="comment">//p和p2都是一种无名结构，里面有x和y</span></span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">ponit</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> x;</span><br><span class="line">    <span class="keyword">int</span> y;</span><br><span class="line">&#125;p1,p2;<span class="comment">//p和p2都是ponit的结构，里面有x和y</span></span><br></pre></td></tr></table></figure>

<h3 id="结构的初始化"><a href="#结构的初始化" class="headerlink" title="结构的初始化"></a>结构的初始化</h3><p>两种方式</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span>&#123;</span></span><br><span class="line">    <span class="keyword">int</span> month;</span><br><span class="line">    <span class="keyword">int</span> day;</span><br><span class="line">    <span class="keyword">int</span> year;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span> <span class="title">today</span> = &#123;</span><span class="number">07</span>,<span class="number">31</span>,<span class="number">2014</span>&#125;;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span> <span class="title">thismonth</span> = &#123;</span>.month=<span class="number">7</span>, .year=<span class="number">2014</span>&#125;;<span class="comment">//此时day为0</span></span><br></pre></td></tr></table></figure>

<h3 id="结构成员"><a href="#结构成员" class="headerlink" title="结构成员"></a>结构成员</h3><p>数组的单元必须是相同类型，结构的成员可以是不同类型</p>
<p>访问成员</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> <span class="keyword">const</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">date</span>&#123;</span></span><br><span class="line">        <span class="keyword">int</span> month;</span><br><span class="line">        <span class="keyword">int</span> day;</span><br><span class="line">        <span class="keyword">int</span> year;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">date</span> <span class="title">today</span> = &#123;</span><span class="number">07</span>,<span class="number">31</span>,<span class="number">2014</span>&#125;;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(today.day);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="结构运算"><a href="#结构运算" class="headerlink" title="结构运算"></a>结构运算</h3><p>要访问整个结构，直接用结构变量的名字</p>
<p>对于整个结构，可以做赋值、取地址，也可以传递给函数参数</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">p1 = (struct ponit)&#123;<span class="number">5</span>,<span class="number">10</span>&#125;;<span class="comment">//相当于p1.x=5;p1.y=10;</span></span><br><span class="line">p1=p2;<span class="comment">//相当于p1.x=p2.x; p1.y=p2.y</span></span><br></pre></td></tr></table></figure>

<h3 id="结构指针"><a href="#结构指针" class="headerlink" title="结构指针"></a>结构指针</h3><p>结构变量的名字并不是结构变量的地址，必须使用&amp;运算符</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span> <span class="title">today</span>;</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span> *<span class="title">pDate</span> = &amp;<span class="title">today</span>;</span></span><br></pre></td></tr></table></figure>

<h3 id="结构与函数"><a href="#结构与函数" class="headerlink" title="结构与函数"></a>结构与函数</h3><h4 id="结构作为函数的参数"><a href="#结构作为函数的参数" class="headerlink" title="结构作为函数的参数"></a>结构作为函数的参数</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">numberOfDays</span><span class="params">(struct date d)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>整个结构可以作为参数的值传入函数</li>
<li>在函数内<strong>新建</strong>一个结构变量，并<strong>复制</strong>调用者的结构的值</li>
<li>也可以返回一个结构</li>
</ul>
<h4 id="结构指针作为参数"><a href="#结构指针作为参数" class="headerlink" title="结构指针作为参数"></a>结构指针作为参数</h4><blockquote>
<p>If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure. ——K&amp;R</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span> &#123;</span></span><br><span class="line">    <span class="keyword">int</span> month;</span><br><span class="line">    <span class="keyword">int</span> day;</span><br><span class="line">    <span class="keyword">int</span> year;</span><br><span class="line">&#125; myday;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">date</span> *<span class="title">p</span> = &amp;<span class="title">myday</span>;</span></span><br><span class="line">(*p).month = <span class="number">12</span>;<span class="comment">//方法一</span></span><br><span class="line">p-&gt;month = <span class="number">12</span>;<span class="comment">//方法二</span></span><br></pre></td></tr></table></figure>

<p>用<code>-&gt;</code>表示指针所指的结构变量中的成员</p>
<h4 id="结构作为函数的返回值"><a href="#结构作为函数的返回值" class="headerlink" title="结构作为函数的返回值"></a>结构作为函数的返回值</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">point</span> &#123;</span></span><br><span class="line">    <span class="keyword">int</span> x;</span><br><span class="line">    <span class="keyword">int</span> y;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">struct <span class="built_in">point</span> <span class="title">getstruct1</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">ponit</span> <span class="title">p</span>;</span></span><br><span class="line">    scanf_s(<span class="string">"%d"</span>,&amp;p.x);</span><br><span class="line">    scanf_s(<span class="string">"%d"</span>,&amp;p.y);</span><br><span class="line">    <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">struct <span class="built_in">point</span>* <span class="title">getstruct2</span><span class="params">(struct ponit *p)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">ponit</span> <span class="title">p</span>;</span></span><br><span class="line">    scanf_s(<span class="string">"%d"</span>,&amp;p-&gt;x);</span><br><span class="line">    scanf_s(<span class="string">"%d"</span>,&amp;p-&gt;y);</span><br><span class="line">    <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="位操作"><a href="#位操作" class="headerlink" title="位操作"></a>位操作</h2><h2 id="C-预处理器和-C-库"><a href="#C-预处理器和-C-库" class="headerlink" title="C 预处理器和 C 库"></a>C 预处理器和 C 库</h2><h2 id="高级数据表示"><a href="#高级数据表示" class="headerlink" title="高级数据表示"></a>高级数据表示</h2>
    </div>

    
    
    
      


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/C/" rel="tag"><i class="fa fa-tag"></i> C</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/05/25/408/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="prev" title="数据结构">
      <i class="fa fa-chevron-left"></i> 数据结构
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/26/Java/Servlet/" rel="next" title="Servlet">
      Servlet <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#C语言概述"><span class="nav-number">1.</span> <span class="nav-text">C语言概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#include-指令和头文件"><span class="nav-number">1.1.</span> <span class="nav-text">#include 指令和头文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#main-函数"><span class="nav-number">1.2.</span> <span class="nav-text">main()函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注释"><span class="nav-number">1.3.</span> <span class="nav-text">注释</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#花括号、函数体和块"><span class="nav-number">1.4.</span> <span class="nav-text">花括号、函数体和块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#声明"><span class="nav-number">1.5.</span> <span class="nav-text">声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#赋值"><span class="nav-number">1.6.</span> <span class="nav-text">赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#printf-函数"><span class="nav-number">1.7.</span> <span class="nav-text">printf()函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#return-语句"><span class="nav-number">1.8.</span> <span class="nav-text">return 语句</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据"><span class="nav-number">2.</span> <span class="nav-text">数据</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#整数和浮点数"><span class="nav-number">2.1.</span> <span class="nav-text">整数和浮点数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-语言基本数据类型"><span class="nav-number">2.2.</span> <span class="nav-text">C 语言基本数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#int-类型"><span class="nav-number">2.2.1.</span> <span class="nav-text">int 类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#声明-int-变量"><span class="nav-number">2.2.1.1.</span> <span class="nav-text">声明 int 变量</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#打印-int-值"><span class="nav-number">2.2.1.2.</span> <span class="nav-text">打印 int 值</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#八进制和十六进制"><span class="nav-number">2.2.1.3.</span> <span class="nav-text">八进制和十六进制</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#其他整数类型"><span class="nav-number">2.2.1.4.</span> <span class="nav-text">其他整数类型</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#char类型"><span class="nav-number">2.2.2.</span> <span class="nav-text">char类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#声明-char-类型变量"><span class="nav-number">2.2.2.1.</span> <span class="nav-text">声明 char 类型变量</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#字符常量和初始化"><span class="nav-number">2.2.2.2.</span> <span class="nav-text">字符常量和初始化</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#符号常量和-const常量"><span class="nav-number">2.2.2.3.</span> <span class="nav-text">符号常量和 const常量</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#非打印字符"><span class="nav-number">2.2.2.4.</span> <span class="nav-text">非打印字符</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Bool-类型"><span class="nav-number">2.2.3.</span> <span class="nav-text">_Bool 类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#可移植类型：stdint-h-和-inttypes-h"><span class="nav-number">2.2.4.</span> <span class="nav-text">可移植类型：stdint.h 和 inttypes.h</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#float、double-和-long-double"><span class="nav-number">2.2.5.</span> <span class="nav-text">float、double 和 long double</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#类型大小"><span class="nav-number">2.2.6.</span> <span class="nav-text">类型大小</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#强制类型转换"><span class="nav-number">2.3.</span> <span class="nav-text">强制类型转换</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串和格式化输入-输出"><span class="nav-number">3.</span> <span class="nav-text">字符串和格式化输入&#x2F;输出</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串简介"><span class="nav-number">3.1.</span> <span class="nav-text">字符串简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#char-类型数组和-null-字符"><span class="nav-number">3.2.</span> <span class="nav-text">char 类型数组和 null 字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#strlen-函数"><span class="nav-number">3.3.</span> <span class="nav-text">strlen()函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常量和-C-预处理器"><span class="nav-number">3.4.</span> <span class="nav-text">常量和 C 预处理器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#printf-和-scanf"><span class="nav-number">3.5.</span> <span class="nav-text">printf()和 scanf()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运算符、表达式和语句"><span class="nav-number">4.</span> <span class="nav-text">运算符、表达式和语句</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基本运算符"><span class="nav-number">4.1.</span> <span class="nav-text">基本运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#其他运算符"><span class="nav-number">4.2.</span> <span class="nav-text">其他运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表达式和语句"><span class="nav-number">4.3.</span> <span class="nav-text">表达式和语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#类型转换"><span class="nav-number">4.4.</span> <span class="nav-text">类型转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#带参数的函数"><span class="nav-number">4.5.</span> <span class="nav-text">带参数的函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#循环"><span class="nav-number">5.</span> <span class="nav-text">循环</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#while循环"><span class="nav-number">5.1.</span> <span class="nav-text">while循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for-循环"><span class="nav-number">5.2.</span> <span class="nav-text">for 循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#出口条件循环：do-while"><span class="nav-number">5.3.</span> <span class="nav-text">出口条件循环：do while</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分支和跳转"><span class="nav-number">6.</span> <span class="nav-text">分支和跳转</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#if-语句"><span class="nav-number">6.1.</span> <span class="nav-text">if 语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#if-else-语句"><span class="nav-number">6.2.</span> <span class="nav-text">if else 语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#逻辑运算符"><span class="nav-number">6.3.</span> <span class="nav-text">逻辑运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#条件运算符"><span class="nav-number">6.4.</span> <span class="nav-text">条件运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#continue-和-break"><span class="nav-number">6.5.</span> <span class="nav-text">continue 和 break</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多重选择：switch-和-break"><span class="nav-number">6.6.</span> <span class="nav-text">多重选择：switch 和 break</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#goto-语句"><span class="nav-number">6.7.</span> <span class="nav-text">goto 语句</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符输入-输出和输入验证"><span class="nav-number">7.</span> <span class="nav-text">字符输入&#x2F;输出和输入验证</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#单字符-I-O：getchar-和-putchar"><span class="nav-number">7.1.</span> <span class="nav-text">单字符 I&#x2F;O：getchar()和 putchar()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#缓冲区"><span class="nav-number">7.2.</span> <span class="nav-text">缓冲区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结束键盘输入"><span class="nav-number">7.3.</span> <span class="nav-text">结束键盘输入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重定向和文件"><span class="nav-number">7.4.</span> <span class="nav-text">重定向和文件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数"><span class="nav-number">8.</span> <span class="nav-text">函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#常用math函数"><span class="nav-number">8.1.</span> <span class="nav-text">常用math函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#fabs-double-x"><span class="nav-number">8.1.1.</span> <span class="nav-text">fabs(double x)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#floor（double-x）和-ceil（double-x）"><span class="nav-number">8.1.2.</span> <span class="nav-text">floor（double x）和 ceil（double x）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#pow-double-r-double-p"><span class="nav-number">8.1.3.</span> <span class="nav-text">pow(double r,double p)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sqrt（double-x）"><span class="nav-number">8.1.4.</span> <span class="nav-text">sqrt（double x）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#log（double-x）"><span class="nav-number">8.1.5.</span> <span class="nav-text">log（double x）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#sin（-double-x）、cos（-double-x）和tan（-double-x）"><span class="nav-number">8.1.6.</span> <span class="nav-text">sin（ double x）、cos（ double x）和tan（ double x）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#asin（double-x），-acos（double-x）F-atan-（double-x"><span class="nav-number">8.1.7.</span> <span class="nav-text">asin（double x）， acos（double x）F atan （double x)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#round（double-x）"><span class="nav-number">8.1.8.</span> <span class="nav-text">round（double x）</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数组和指针"><span class="nav-number">9.</span> <span class="nav-text">数组和指针</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数组"><span class="nav-number">9.1.</span> <span class="nav-text">数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多维数组"><span class="nav-number">9.2.</span> <span class="nav-text">多维数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#指针和数组"><span class="nav-number">9.3.</span> <span class="nav-text">指针和数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#函数、数组和指针"><span class="nav-number">9.4.</span> <span class="nav-text">函数、数组和指针</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#指针操作"><span class="nav-number">9.5.</span> <span class="nav-text">指针操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串和字符串函数"><span class="nav-number">10.</span> <span class="nav-text">字符串和字符串函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#枚举"><span class="nav-number">11.</span> <span class="nav-text">枚举</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#技巧：自动计数的枚举"><span class="nav-number">11.1.</span> <span class="nav-text">技巧：自动计数的枚举</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#指定枚举量的值"><span class="nav-number">11.2.</span> <span class="nav-text">指定枚举量的值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#存储类别、链接和内存管理"><span class="nav-number">12.</span> <span class="nav-text">存储类别、链接和内存管理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文件输入-输出"><span class="nav-number">13.</span> <span class="nav-text">文件输入&#x2F;输出</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结构和其他数据形式"><span class="nav-number">14.</span> <span class="nav-text">结构和其他数据形式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#声明结构类型"><span class="nav-number">14.1.</span> <span class="nav-text">声明结构类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构的初始化"><span class="nav-number">14.2.</span> <span class="nav-text">结构的初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构成员"><span class="nav-number">14.3.</span> <span class="nav-text">结构成员</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构运算"><span class="nav-number">14.4.</span> <span class="nav-text">结构运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构指针"><span class="nav-number">14.5.</span> <span class="nav-text">结构指针</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构与函数"><span class="nav-number">14.6.</span> <span class="nav-text">结构与函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#结构作为函数的参数"><span class="nav-number">14.6.1.</span> <span class="nav-text">结构作为函数的参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#结构指针作为参数"><span class="nav-number">14.6.2.</span> <span class="nav-text">结构指针作为参数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#结构作为函数的返回值"><span class="nav-number">14.6.3.</span> <span class="nav-text">结构作为函数的返回值</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#位操作"><span class="nav-number">15.</span> <span class="nav-text">位操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#C-预处理器和-C-库"><span class="nav-number">16.</span> <span class="nav-text">C 预处理器和 C 库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#高级数据表示"><span class="nav-number">17.</span> <span class="nav-text">高级数据表示</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Lanqilu"
      src="http://img.whl123456.top/image/avatar.jpg">
  <p class="site-author-name" itemprop="name">Lanqilu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">85</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/lanqilu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lanqilu" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=zqKvoL_noruOqKG2o6_nouCtoaM" title="E-Mail → http:&#x2F;&#x2F;mail.qq.com&#x2F;cgi-bin&#x2F;qm_share?t&#x3D;qm_mailme&amp;email&#x3D;zqKvoL_noruOqKG2o6_nouCtoaM" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lanqilu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

  

    </div>
</body>
</html>
